package com.mail.server.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.api.UmsResourceApi;
import com.mail.model.db.UmsResource;
import com.mail.model.dto.ResouceDto;
import com.mail.model.vo.MyPage;
import com.mail.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 吕永乐
 * @date 2022/06/23 11:39
 **/
@Service
public class UmsResourceService {

	@DubboReference
	private UmsResourceApi umsResourceApi;

	/**
	 * 查询所有后台资源
	 * @return
	 */
	public List<UmsResource> listAll() {
		List<UmsResource> umsResources = umsResourceApi.getAll();
		if(CollUtil.isEmpty(umsResources)){
			return new ArrayList<>();
		}
		return umsResources;
	}

	/**
	 * 分页模糊查询后台资源
	 * @param pageNum
	 * @param pageSize
	 * @param categoryId
	 * @param nameKeyword
	 * @param urlKeyword
	 * @return
	 */
	public MyPage<UmsResource> listByPage(Long pageNum, Long pageSize, Long categoryId, String nameKeyword, String urlKeyword) {
		Page<UmsResource> page = umsResourceApi.listByPage(pageNum, pageSize, categoryId, nameKeyword, urlKeyword);
		return MyPage.init(page);
	}

	// 添加后台资源
	public void createResource(UmsResource umsResource) {
		umsResource.setCreateTime(LocalDateTime.now());
		umsResourceApi.createResource(umsResource);
	}

	// 根据ID删除后台资源
	public void deleteResourceById(Long id) {
		umsResourceApi.deleteResourceById(id);
	}

	// 修改后台资源
	public void updateResource(Long id,UmsResource umsResource) {
		umsResourceApi.updateResource(id,umsResource);
	}

	// 根据ID获取资源详情
	public UmsResource queryResourceById(Long id) {
		UmsResource umsResource = umsResourceApi.queryResourceById(id);
		return umsResource;
	}
}
